﻿using Assistant.Report.Preview;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using WebAssistant.BusinessLogic;
using WebAssistant.BusinessLogic.Model;
using static Assistant.Controllers.PrintModel;

namespace Assistant.Controllers
{
    [ApiController]
    public class PrintController : ControllerBase
    {
        private readonly ILogger<PrintController> _logger;
        private PrintAction action;
        private RequestResult requestResult;


        public PrintController(ILogger<PrintController> logger)
        {
            _logger = logger;
        }

        [HttpPost]
        [Consumes("application/json")]
        [Route("~/api/interfaces/printing_services")]
        public PrinterParameter GetPrintingServicesByKey(string key)
        {
            Interfaces interfaces = DataAccess.GetInterfaceByKey(key);
            PrinterParameter pp = new PrinterParameter();
            if (interfaces != null && interfaces.State)
            {
                pp.run = true;
            }
            else
            {
                pp.run = false;
            }

            if (DataAccess.CheckPrinterExists(key))
            {
                Printers printerParameter = DataAccess.GetPrinterOptionsByKey(key);
                pp.name = printerParameter.Default;
                pp.key = printerParameter.Key;
                pp.mode = printerParameter.Mode;
                pp.silent = printerParameter.Silent;
            }
            return pp;
        }

        [HttpPost]
        [Consumes("application/json")]
        [Route("~/api/interfaces/request_print")]
        public RequestResult RequestPrintReport([FromBody] PrintAction printAction)
        {
            requestResult = new RequestResult();

            string active_model = printAction.Context.active_model;
            if (OdooRpcClient.OdooRequest.ExistentialModel(active_model))
            {
                //通过key获取打印服务是否运行
                bool IsRun = DataAccess.GetRunPrintServerByKey(printAction.Service);

                if (IsRun)
                {
                    //运行了服务,则获取打印机的属性
                    Printers printer = DataAccess.GetPrinterOptionsByKey(printAction.Service);
                    if (printer.Silent)
                    {
                        //静默打印
                    }
                    else
                    {
                        action = printAction;
                        if (printer.Mode == 0)
                        {
                            //对话框打印
                        }
                        else if (printer.Mode == 1)
                        {                      
                            using (FormStartPreview formPreview = new FormStartPreview(printAction))
                            {
                                formPreview.ShowDialog();
                            }

                        }
                        //pr.mode = printer.Mode;
                    }
                }
                else
                {
                    //未运行服务
                    requestResult.errcode = 2;
                }
                requestResult.errmsg = RequestResult.GetEnumDesc((ErrorCode)requestResult.errcode);
            }
            else
            {
                requestResult.errcode = 4;
            }           
            
            return requestResult;
        }

        public class PrinterParameter
        {
            public bool run { get; set; }

            public string key { get; set; }

            public string name { get; set; }

            public int mode { get; set; }

            public bool silent { get; set; }
        }

        public class PrintResult
        {
            public bool using_templates { get; set; }

            public bool is_finish { get; set; }

            public int mode { get; set; }

            public object print_options { get; set; }
        }        
    }
}
